package gui;

import java.awt.event.*;
import java.io.*;
import java.util.*;
import netPacks.*;

import javax.swing.*;

import player.*;

public class gameGui extends WindowAdapter implements WindowListener, ActionListener, Runnable {
	
	int GAME_DUR = 300000;
	boolean quit = false;
    public gameGui(){
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private static volatile gameGui instance = null;
    
    public static gameGui getInstance() {
        if (instance == null){
            synchronized (gameGui.class){
                    if (instance == null){
                        instance = new gameGui();
                    }
        }}
        return instance;
    }
    
    private void initComponents() {
    	gameFrame = new javax.swing.JFrame();
        incomingWord = new javax.swing.JTextField();
        label1 = new java.awt.Label();
        guessWord = new javax.swing.JTextField();
        guessButton = new javax.swing.JButton();
        label2 = new java.awt.Label();
        countdown = new java.text.SimpleDateFormat("mm : ss");
        countdownClock = new javax.swing.JLabel(countdown.format(new Date(GAME_DUR)),JLabel.CENTER);
        forfButton = new javax.swing.JButton();
        label3 = new java.awt.Label();
        tries = new javax.swing.JTextField();
        label4 = new java.awt.Label();
        opName = new javax.swing.JTextField();
        
        ActionListener al = new ActionListener(){
        	long x = GAME_DUR - 1000;
        public void actionPerformed(ActionEvent ae){
          countdownClock.setText(countdown.format(new Date(x)));
          x -= 1000;
          player.getInstance().setTimeLeft(x);}};
        timer = new javax.swing.Timer(1000, al);

        gameFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        gameFrame.setResizable(false);

        incomingWord.setEditable(false);
        incomingWord.setBackground(new java.awt.Color(11, 22, 16));
        incomingWord.setFont(new java.awt.Font("Felix Titling", 1, 36)); // NOI18N
        incomingWord.setForeground(new java.awt.Color(255, 255, 255));
        incomingWord.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        incomingWord.setText("Waiting for incoming...");
        incomingWord.setFocusable(false);
        incomingWord.setVerifyInputWhenFocusTarget(false);
        incomingWord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                incomingWordActionPerformed(evt);
            }
        });

        label1.setAlignment(java.awt.Label.CENTER);
        label1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N

        guessWord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guessWordActionPerformed(evt);
            }
        });
        guessWord.addFocusListener(new FocusListener(){
            @Override
            public void focusGained(FocusEvent e){
                guessWord.setText("");
            }

			@Override
			public void focusLost(FocusEvent e) {
				//do absolutely nothing...
			}
            });
        guessWord.setEditable(false);

        guessButton.setFont(new java.awt.Font("Tahoma", 2, 18)); // NOI18N

        label2.setAlignment(java.awt.Label.CENTER);
        label2.setForeground(new java.awt.Color(255, 0, 0));
        label2.setText("Time remaining:");

        forfButton.setFont(new java.awt.Font("Serif", 1, 14)); // NOI18N
        forfButton.setText("Forfeit");

        label3.setAlignment(java.awt.Label.CENTER);
        label3.setFont(new java.awt.Font("Elephant", 2, 24)); // NOI18N
        label3.setForeground(new java.awt.Color(100, 105, 160));
        label3.setText("Tries:");

        tries.setEditable(false);
        tries.setFont(new java.awt.Font("Elephant", 2, 24)); // NOI18N
        tries.setText("3");

        label4.setText("Your opponent:");
        
        opName.setText("");
        opName.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(gameFrame.getContentPane());
        gameFrame.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(215, 215, 215))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(guessWord, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 487, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(incomingWord, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 487, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(180, 180, 180)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(tries, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(guessButton, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(2, 2, 2)
                                    .addComponent(opName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGap(23, 23, 23)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(forfButton)
                        .addGap(225, 225, 225))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(countdownClock, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(countdownClock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(opName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addComponent(incomingWord, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(guessWord, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(guessButton, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addComponent(forfButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tries)
                    .addComponent(label3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(48, Short.MAX_VALUE))
        );


        gameFrame.pack();
        gameFrame.setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents
    
    public void setIncomingWordText(String s){
    	this.incomingWord.setText(s);
    }
    
    public String getIncomingWordText(){
    	return this.incomingWord.getText();
    }
    
    public void setGuessWordEditable(boolean x){
    	this.guessWord.setEditable(x);
    }
    
    public void setGuessButtonEnable(boolean x){
    	this.guessButton.setEnabled(x);
    }
    
    public void startTimer(){
    	timer.start();
    }
  
    protected void incomingWordActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		
	}

	protected void guessWordActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		
	}
	
	public synchronized void windowIconified(WindowEvent e) {
		
	}

	public void display(){
        this.gameFrame.setVisible(true);
    }
	
	public void terminateGame(){
		this.gameFrame.dispose();
	}
	
	public void setOpName(String name){
		this.opName.setText(name);
	}

        /* Create and display the form */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton guessButton;
    private javax.swing.JButton forfButton;
    private javax.swing.JTextField incomingWord;
    private javax.swing.JTextField guessWord;
    private java.text.SimpleDateFormat countdown;
    private javax.swing.JLabel countdownClock;
    private javax.swing.JTextField tries;
    private javax.swing.JTextField opName;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private java.awt.Label label4;
    private javax.swing.JFrame gameFrame;
    private javax.swing.Timer timer;
    // End of variables declaration//GEN-END:variables

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void setGuiMode(){
		if(player.getInstance().getTurn()){
			guessWord.setText("Type your word here");
			guessWord.setEditable(true);
			guessButton.setText("SEND!");
			incomingWord.setText("Make it Challenging!");
			label1.setText("Click SEND! When Ready...");
			guessButton.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                String tmp = guessWord.getText();
	                message msg = new message();
	                msg.setAction("aWord");
	                msg.setData(tmp);
	                try {
						player.getInstance().getP2PCon().gameOos.writeObject(msg);
					} 
	                catch (IOException e) {
						System.err.println("Cannot send word to opponent: \n"+e.getMessage());
					}
	                timer.start();
	                guessWord.setEditable(false);
	                guessButton.setEnabled(false);
	                incomingWord.setText("WAIT FOR RESPONSE");
	            }
	        });
		}
		else{
			guessWord.setText("Type your guess here...");
			guessButton.setText("Guess!");
			guessButton.setEnabled(false);
			incomingWord.setText("Waiting for incoming...");
			label1.setText("Guess It!");
			guessButton.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                String tmp = guessWord.getText();
	                if(player.getInstance().cmpr.compare(tmp, player.getInstance().getSentence())){
	                	timer.stop();
	                	player.getInstance().setScore();
	                }
	                else{
	                	label1.setText("Guess Again!");
	                	player.getInstance().setTries(player.getInstance().getTries()-1);
	                	tries.setText(Integer.toString(player.getInstance().getTries()));
	                	
	                }
	            }
	        });
		}
		}
	public void run() {
		while(!this.quit){
		}
	}	
}
